{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import psycopg2\n",
    "\n",
    "conn = psycopg2.connect(service='firecares')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "with conn.cursor() as c:\n",
    "    c.execute(\"\"\"\n",
    "create table if not exists department_population_samples (\n",
    "    id serial primary key,\n",
    "    department_id integer not null,\n",
    "    population integer,\n",
    "    sample_date timestamp with time zone not null default now()\n",
    ");\n",
    "\"\"\")\n",
    "    \n",
    "conn.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "with conn.cursor() as c:\n",
    "    c.execute(\"\"\"select id, population from firestation_firedepartment;\"\"\")\n",
    "    res = c.fetchall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "with conn.cursor() as c:\n",
    "    for r in res:\n",
    "        c.execute(\"insert into department_population_samples (department_id, population) values (%(dept)s, %(pop)s)\",\n",
    "                 dict(dept=r[0], pop=r[1]))\n",
    "\n",
    "conn.commit()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
